#! /bin/bash

# This file is part of IVRE.
# Copyright 2011 - 2014 Pierre LALET <pierre.lalet@cea.fr>
#
# IVRE is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# IVRE is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
# License for more details.
#
# You should have received a copy of the GNU General Public License
# along with IVRE. If not, see <http://www.gnu.org/licenses/>.

for f in doc/*.md ; do
    out=`echo ${f/.md/.txt} | tr 'A-Z' 'a-z'`
    pandoc -f markdown_github -t native $f \
	| python2 -c "print __import__('re').sub('([,\\[](?:Link|Image) \\[[^\\]]*\\] \\(\")([^\\\"]*)(\",\"[^\\\"]*\"\\))', lambda x: x.groups()[0] + (('doc:' if x.groups()[1].split('#', 1)[0] else '') + ('#'.join([x.groups()[1].split('#', 1)[0].replace('.md', ''), x.groups()[1].split('#', 1)[1].replace('-', '_')]) if '#' in x.groups()[1] else x.groups()[1].replace('.md', '').replace('/', ':')) if ':' not in x.groups()[1] else x.groups()[1]) + x.groups()[2], __import__('sys').stdin.read())" \
	| pandoc -f native -t dokuwiki \
	| sed 's#\\\\ # #g' \
	> "web/dokuwiki/$out"
done
